package com.xiaoma.service.impl;

import com.alibaba.fastjson.JSON;
import com.xiaoma.dao.GoodsDao;
import com.xiaoma.entity.Cart;
import com.xiaoma.service.CartService;
import entity.Goods;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @Author: xiaoma
 * @Date: 2021/9/17 15:02
 */
@Service
public class CartServiceImpl implements CartService {

    private static final String CartKey =  "cart:";

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private GoodsDao goodsDao;
    /**
     * @Description  添加商品到购物车
     * @Author Xiaoma
     * @Date 2021/9/17 13:56
     * @param id
     * @param num
     * @param request
     * @return com.xiaoma.entity.CartDetail
     **/
    @Override
    public Cart addCart(String id, Integer num, HttpServletRequest request) {
        //购物车 cart:1  gid  json
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(CartKey + uid);
        String json = ops.get(id);
        if (StringUtils.isEmpty(json)){
            //添加到购物车
            Goods goods = goodsDao.selectById(id);
            Cart cartDetail = new Cart().setCount(num).setGoods(goods);
            ops.put(id, JSON.toJSONString(cartDetail));
            return cartDetail;
        }
        else {
            //购物车数量+1
            Cart cartDetail = JSON.parseObject(json, Cart.class);
            cartDetail.setCount(cartDetail.getCount()+num);
            ops.put(id, JSON.toJSONString(cartDetail));
            return cartDetail;
        }

    }
    /**
     * @Description  查询该用户下的购物车信息
     * @Author Xiaoma
     * @Date 2021/9/17 15:50
     * @param request
     * @return java.util.List<com.xiaoma.entity.CartDetail>
     **/
    @Override
    public List<Cart> queryCart(HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(CartKey + uid);
        List<Cart> res = ops.values().stream().map((x) -> JSON.parseObject(x, Cart.class)).collect(Collectors.toList());
        return res;
    }
    /**
     * @Description  更改选中状态
     * @Author Xiaoma
     * @Date 2021/9/17 15:54
     * @param id
     * @param isChecked
     * @return com.xiaoma.entity.CartDetail
     **/
    @Override
    public Cart checkItem(String id, Integer isChecked, HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(CartKey + uid);
        String json = ops.get(id);
        Cart cartDetail = JSON.parseObject(json, Cart.class);
        cartDetail.setCheck(isChecked==1?true:false);
        ops.put(id,JSON.toJSONString(cartDetail));
        return cartDetail;
    }
   /**
    * @Description 删除购物车项
    * @Author Xiaoma
    * @Date 2021/9/17 16:02
    * @param id
    * @return java.lang.Long
    **/
    @Override
    public Long deleteItem(String id,HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(CartKey + uid);
        BoundSetOperations<String, String> ops2 = redisTemplate.boundSetOps(CartKey + uid);
        return ops.delete(id);
    }
}
